import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/leave_type_entity.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/input/input_field.dart';
import 'package:finance_app/src/widgets/pop/normal_pop.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'default_bottom_sheet.dart';

/// 添加假期
/// result
/// {
///       'amount': amountController.text,
///       'leaveType': leaveTypeController.text,
///     }
showAddLeaveTypeBottomSheet(
    BuildContext context, String amount, LeaveTypeEntity leaveType) async {
  final result = await showDefaultBottomSheet(context,
      child: AddDiscountBottomSheet(
        amount: amount,
        leaveType: leaveType,
      ),
      title: S.current.add_leave_type,
      isScrollControlled: true,
      leading: amount == null
          ? null
          : DefaultBottomSheetLeadingButton(
              title: S.current.delete,
              onTap: amount == null
                  ? null
                  : () {
                      Navigator.of(context).pop(true);
                    },
            ));
  return result;
}

class AddDiscountBottomSheet extends StatefulWidget {
  final String amount;
  final LeaveTypeEntity leaveType;

  const AddDiscountBottomSheet({Key key, this.amount, this.leaveType})
      : super(key: key);

  @override
  _AddDiscountBottomSheetState createState() => _AddDiscountBottomSheetState();
}

class _AddDiscountBottomSheetState extends State<AddDiscountBottomSheet> {
  TextEditingController amountController = TextEditingController();
  FocusNode amountFocusNode = FocusNode();

  LeaveTypeEntity leaveTypeEntity;

  List<String> leaveTypeList;
  bool isEnable = false;

  void _onAdd() {
    if (amountController.text.length == 0) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_enter_the_amount);
      return;
    }
    if (leaveTypeEntity == null) {
      BaseConfig.dialogBase.showToast(
          context: context, content: S.current.please_select_the_leave_type);
      return;
    }
    Navigator.of(context).pop({
      'amount': amountController.text,
      'leaveType': leaveTypeEntity,
    });
  }

  void _onSelectLeaveType(String value) {
    _handleUpdateListener();
    leaveTypeEntity = Config.settingData.leaveType
        .firstWhere((element) => element.name == value, orElse: () => null);
    setState(() {});
  }

  @override
  void initState() {
    super.initState();
    if (widget.amount != null) {
      amountController.text = widget.amount;
    }
    leaveTypeList = Config.settingData.leaveType.map((e) => e.name).toList();
    leaveTypeEntity = widget.leaveType;
    amountController.addListener(_handleUpdateListener);
  }

  @override
  void dispose() {
    super.dispose();
    amountController.dispose();
    amountFocusNode.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        AppSpaces.h20,
        NormalPop<String>(
          select: leaveTypeEntity?.name,
          group: leaveTypeList,
          buildDisplayName: (e) => e,
          hint: S.current.leave_type,
          isTop: true,
          onSelect: _onSelectLeaveType,
        ),
        AppSpaces.h15,
        InputField(
          tip: S.current.duration_days,
          controller: amountController,
          focusNode: amountFocusNode,
          keyboardType: TextInputType.number,
          inputFormatters: [
            FilteringTextInputFormatter.allow(RegExp(r'^[0-9]+$')),
          ],
        ),
        AppSpaces.h30,
        NormalButton(
          title: S.current.add,
          onTap: isEnable ? _onAdd : null,
        ),
      ],
    );
  }

  void _handleUpdateListener() {
    if (amountController.text.length > 0 && leaveTypeEntity != null) {
      if (isEnable == false) {
        setState(() {
          isEnable = true;
        });
      }
    } else {
      if (isEnable == true) {
        setState(() {
          isEnable = false;
        });
      }
    }
  }
}
